//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.thinkgem.jeesite.common.wechat.utils;

import com.thinkgem.jeesite.common.wechat.constants.Constants;
import com.thinkgem.jeesite.common.wechat.pojo.AccessToken;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class WeixinUtil {
	public WeixinUtil() {
	}

	public static JSONObject HttpRequest(String request, String RequestMethod, String output) {
		JSONObject jsonObject = null;
		StringBuffer buffer = new StringBuffer();

		try {
			URL e = new URL(request);
			HttpURLConnection connection = (HttpURLConnection)e.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.setRequestMethod(RequestMethod);
			OutputStream input;
			if(output != null) {
				input = connection.getOutputStream();
				input.write(output.getBytes("UTF-8"));
				input.close();
			}

			InputStream input1 = connection.getInputStream();
			InputStreamReader inputReader = new InputStreamReader(input1, "UTF-8");
			BufferedReader reader = new BufferedReader(inputReader);

			String line;
			while((line = reader.readLine()) != null) {
				buffer.append(line);
			}

			reader.close();
			inputReader.close();
			input1.close();
			input = null;
			connection.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
		} catch (Exception var11) {
			;
		}

		return jsonObject;
	}

	public static AccessToken getAccessToken(String corpID, String secret) {
		AccessToken accessToken = null;
		String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID&corpsecret=SECRET";
		String requestUrl = access_token_url.replace("CorpID", corpID).replace("SECRET", secret);
		JSONObject jsonObject = HttpRequest(requestUrl, "GET", (String)null);
		if(null != jsonObject) {
			try {
				accessToken = new AccessToken();
				accessToken.setToken(jsonObject.getString("access_token"));
				accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
			} catch (Exception var8) {
				accessToken = null;
				String error = String.format("获取token失败 errcode:{} errmsg:{}", new Object[]{Integer.valueOf(jsonObject.getInt("errcode")), jsonObject.getString("errmsg")});
			}
		}

		return accessToken;
	}

	public static void getAgentid(String access_token, String agentid) {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token=ACCESS_TOKEN&agentid=AGENTID";
		url = url.replace("ACCESS_TOKEN", access_token).replace("AGENTID", agentid);
		JSONObject jsonObject = HttpRequest(url, "GET", (String)null);
		if(null != jsonObject) {
			System.out.println(jsonObject.toString());
		}

	}

	public static void getAgent(String access_token) {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list?access_token=ACCESS_TOKEN";
		url = url.replace("ACCESS_TOKEN", access_token);
		JSONObject jsonObject = HttpRequest(url, "GET", (String)null);
		if(null != jsonObject) {
			System.out.println(jsonObject.toString());
		}

	}

	public static void addAgentid(String access_token, String output) {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set?access_token=ACCESS_TOKEN";
		url = url.replace("ACCESS_TOKEN", access_token);
		JSONObject jsonObject = HttpRequest(url, "POST", output);
		if(null != jsonObject) {
			System.out.println(jsonObject.toString());
		}

	}

	public static String URLEncoder(String str) {
		String result = str;

		try {
			result = URLEncoder.encode(result, "UTF-8");
		} catch (Exception var3) {
			var3.printStackTrace();
		}

		return result;
	}

	public static void main(String[] args) {
		AccessToken access = getAccessToken(Constants.CORPID, Constants.ENCODINGAESKEY);
		getAgentid(access.getToken(), Constants.AGENTID);
		getAgent(access.getToken());
		String postData = "{\"agentid\": \"%s\",\"agentid\": \"%s\"}";
		postData = String.format(postData, new Object[]{"1", "2"});
		addAgentid(access.getToken(), postData);
	}
}
